var starTag = angular.module("starTag", []);
starTag.directive("star", function() {
	return {
		restrict: "EA",
		templateUrl: 'tag/star/index.html',
		scope: {
			value: '=',edit:'@edit'
		},
		controller: function($scope) {
			
			$scope.value = $scope.value == null ? 0 : $scope.value;
			//监听外部value值得变化
			$scope.$watch('value', function(newValue, oldValue) {
				$scope.updateView();
			});
			//允许编辑
			if($scope.edit==true || $scope.edit=='true'){
				$scope.editStar=function(i) {
					$scope.value=i;
				};
			}
						
		},
		link: function(scope, element, attr) {
			//导入图标库
			var path = "tag/";
			var iconStyle = document.createElement('link');
			iconStyle.setAttribute("rel", "stylesheet");
			iconStyle.setAttribute("type", "text/css");
			iconStyle.setAttribute("href", path + "star/font/iconfont.css");
			document.getElementsByTagName("head")[0].appendChild(iconStyle);

			var spans=element[0].getElementsByClassName('starTag');
			//显示视图
			scope.updateView=function() {
				for(var i = 0; i < 5; i++) {
					var icon = i < scope.value ? '&#xe60a;' : '&#xe605;';
					spans[i].innerHTML=icon;
				}
			}
			scope.updateView();
		}
	}
});